package com.example.professorevaluation

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.professorevaluation.adapter.ProfessorAdapter
import com.example.professorevaluation.bean.ProfessorBean
import com.example.professorevaluation.dao.ProfessorDatabase
import com.example.professorevaluation.databinding.ActivityMainBinding
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var mAdapter: ProfessorAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)
        setSupportActionBar(binding.toolbar)
        val actionBar = supportActionBar
        if (actionBar != null) {
            actionBar.title = "Professors"
        }
        val list: MutableList<ProfessorBean> = ArrayList()
        lifecycleScope.launch {
            if (ProfessorDatabase.getInstance(this@MainActivity).teacherDao().getAll().isNullOrEmpty()) {
                val professorBean = ProfessorBean()
                professorBean.courses = "Advanced Statistics"
                professorBean.name = "J Buckland-Nicks"
                professorBean.university = "St. Francis Xavier University"
                ProfessorDatabase.getInstance(this@MainActivity).teacherDao().insertOne(professorBean)
                val professorBean1 = ProfessorBean()
                professorBean1.courses = "Psychology of Music"
                professorBean1.name = "Peter McCormick"
                professorBean1.university = "St. Francis Xavier University"
                ProfessorDatabase.getInstance(this@MainActivity).teacherDao().insertOne(professorBean1)
                val professorBean2 = ProfessorBean()
                professorBean2.courses = "Modern History"
                professorBean2.name = "Pat Hogan"
                professorBean2.university = "St. Francis Xavier University"
                ProfessorDatabase.getInstance(this@MainActivity).teacherDao().insertOne(professorBean2)
                val professorBean3 = ProfessorBean()
                professorBean3.courses = "Macro-Economics"
                professorBean3.name = "Neil Maltby"
                professorBean3.university = "St. Francis Xavier University"
                ProfessorDatabase.getInstance(this@MainActivity).teacherDao().insertOne(professorBean3)
                val professorBean4 = ProfessorBean()
                professorBean4.courses = "Inorganic Chemistry"
                professorBean4.name = "Stanley Cameron"
                professorBean4.university = "Dalhousie University"
                ProfessorDatabase.getInstance(this@MainActivity).teacherDao().insertOne(professorBean4)
                val professorBean5 = ProfessorBean()
                professorBean5.courses = "Data Science"
                professorBean5.name = "James Pincock"
                professorBean5.university = "Dalhousie University"
                ProfessorDatabase.getInstance(this@MainActivity).teacherDao().insertOne(professorBean5)
                val professorBean6 = ProfessorBean()
                professorBean6.courses = "Microbiology"
                professorBean6.name = "Don Stoltz"
                professorBean6.university = "Dalhousie University"
                ProfessorDatabase.getInstance(this@MainActivity).teacherDao().insertOne(professorBean6)
                val professorBean7 = ProfessorBean()
                professorBean7.courses = "Computer Graphics "
                professorBean7.name = "Norman Scrimger"
                professorBean7.university = "Dalhousie University"
                ProfessorDatabase.getInstance(this@MainActivity).teacherDao().insertOne(professorBean7)
            }
        }
        list.addAll(ProfessorDatabase.getInstance(this).teacherDao().getAll())
        mAdapter = ProfessorAdapter(list, this)
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(this@MainActivity)
            adapter = mAdapter
        }
        mAdapter.notifyDataSetChanged()
        mAdapter.setOnItemClick(object : ProfessorAdapter.OnItemClick{
            override fun data(bean: ProfessorBean) {
                val bundle = Bundle()
                bundle.putString("name", bean.name)
                bundle.putString("course", bean.courses)
                bundle.putString("university", bean.university)
                startActivity(Intent(this@MainActivity, ProfessorDetailActivity::class.java).putExtras(bundle))
            }

        })
    }


}